package pers.mine.interceptors;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import pers.mine.po.GoodsGroup;
import pers.mine.service.inter.GoodsGroupService;
import pers.mine.service.inter.GroupShipService;
import pers.mine.vo.Info;

/**
 * 导航栏中商品分类信息的初始化拦截器
 * 
 * @author Mine
 * @since 2016年11月29日_上午12:28:11
 */
public class GoodsGroupInfoInitInterceptor implements HandlerInterceptor {
	@Autowired
	GoodsGroupService goodsGroupService;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		Info indexInfo = null;// 放到页面的信息
		// 尝试获取session中的admin

		if (request.getSession().getAttribute("simpleGroups") == null
				|| request.getSession().getAttribute("carGroups") == null) {
			HttpSession session = request.getSession();
			try {
				// 商品基本类型List获取
				List<GoodsGroup> simpleGroups = goodsGroupService
						.findBySimpleType();
				session.setAttribute("simpleGroups", simpleGroups);
				// 商品汽车类型List获取
				List<GoodsGroup> carGroups = goodsGroupService.findByCarType();
				// 放入
				session.setAttribute("carGroups", carGroups);
			} catch (Exception e) {
				e.printStackTrace();
				indexInfo = new Info();
				indexInfo.setType(Info.ERROR);
				indexInfo.setMsg("[获取商品分类信息操作]:发生异常...");
				request.setAttribute("indexInfo", indexInfo);
			}
		}
		return true;
	}

	/*
	 * @see
	 * org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO 自动生成的方法存根

	}

	/*
	 * @see
	 * org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO 自动生成的方法存根

	}

}
